package config

import (
	"bufio"
	"cms/model"
	"fmt"
	"github.com/sirupsen/logrus"
	"io"
	"os"
	"strings"
)

type data struct {
	BoardTypes []model.BoardType
}

func initData()  {
	dir, _ := os.Getwd()
	f, err := os.Open(dir + "/data/board_type.txt")
	if err != nil {
		logrus.Panicf("open [data/board_type.txt] error: %v", err)
	}
	defer f.Close()
	rd := bufio.NewReader(f)
	for {
		line, err := rd.ReadString('\n')
		if err != nil || io.EOF == err {
			break
		}
		temps := strings.Split(strings.TrimSpace(line), " ")

		templatePre := ""
		if temps[0] == "page" {
			templatePre = "diy/"
		}
		Data.BoardTypes = append(Data.BoardTypes, model.BoardType{
			Name: temps[0],
			Text: temps[1],
			TemplateIndex: fmt.Sprintf("%s%s/index", templatePre, temps[0]),
			TemplateList: fmt.Sprintf("%s%s/list", templatePre, temps[0]),
			TemplateDetail: fmt.Sprintf("%s%s/detail", templatePre, temps[0]),
		})
	}
}
